package hikari.gui.panels.particle;

import com.gooddies.swing.RowLayout;
import hikari.gui.guicomponents.hLabel;
import hikari.persistence.file.FileSection;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import org.swig.jirr.IParticleBoxEmitter;
import org.swig.jirr.IParticleEmitter;
import org.swig.jirr.IParticleSystemSceneNode;
import org.swig.jirr.IVideoDriver;
import org.swig.jirr.aabbox3df;
import org.swig.jirr.vector3df;

/**
 * @author sad
 */
public class BoxEmitterEditorPanel extends EmitterEditorPanel {

    private IParticleBoxEmitter emmiter;
    private Vector3dEditor size;
    private Vector3dEditor center;

    public BoxEmitterEditorPanel() {
        setTitle("Box Emitter");
        setLayout(new RowLayout().setStartYGap(20).setStartXGap(10).setEndYGap(10));

        add(new hLabel("Emitter sizes"), RowLayout.NewRow);
        size = new Vector3dEditor(this);
        size.setValueChangedEvent(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                sizeChanged();
            }
        });

        add(new hLabel("Emitter position"), RowLayout.NewRow);
        center = new Vector3dEditor(this);
        center.setValueChangedEvent(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                centerChanged();
            }
        });
    }

    private void sizeChanged() {
        vector3df sz = this.size.getValue();
        setEmitterSize(emmiter, sz);
    }

    private static void setEmitterSize(IParticleBoxEmitter emitter, vector3df sz) {
        aabbox3df box = emitter.getBox();
        vector3df tCenter = box.getCenter();
        vector3df min = box.getMinEdge();
        vector3df max = box.getMaxEdge();

        float x = sz.getX() / 2f;
        float y = sz.getY() / 2f;
        float z = sz.getZ() / 2f;

        min.setX(-x + tCenter.getX());
        min.setY(-y + tCenter.getY());
        min.setZ(-z + tCenter.getZ());

        max.setX(x + tCenter.getX());
        max.setY(y + tCenter.getY());
        max.setZ(z + tCenter.getZ());

        box.setMaxEdge(max);
        box.setMinEdge(min);
        emitter.setBox(box);
    }

    private void centerChanged() {
        vector3df pos = center.getValue();
        emmiter.setCenter(pos);
    }

    @Override
    public void putFromEmmiter(IParticleEmitter particleEmitter) {
        emmiter = (IParticleBoxEmitter) particleEmitter;
        aabbox3df box = this.emmiter.getBox();
        center.setValue(box.getCenter());

        size.setValue(getSizes(emmiter));
    }

    public static vector3df getSizes(IParticleBoxEmitter emitter) {
        aabbox3df box = emitter.getBox();
        vector3df min = box.getMinEdge();
        vector3df max = box.getMaxEdge();
        vector3df edges = new vector3df(max.getX() - min.getX(), max.getY() - min.getY(), max.getZ() - min.getZ());
        return edges;
    }

    @Override
    public void putToEmmiter(IParticleEmitter emmiter) {
    }

    @Override
    public void renderEmitter(IVideoDriver driver) {
        aabbox3df box = emmiter.getBox();
        driver.setMaterial(getMaterial());
        driver.draw3DBox(box);
    }

    public static void writeData(FileSection parentSection, IParticleEmitter emitter) throws IOException {
        IParticleBoxEmitter spEmitter = (IParticleBoxEmitter) emitter;
        FileSection sect = parentSection.createNewSection("boxEm");
        sect.writeVector("center", spEmitter.getBox().getCenter());
        sect.writeVector("size", getSizes(spEmitter));
        sect.close();
    }

    public static IParticleBoxEmitter loadData(IParticleSystemSceneNode partSystem, FileSection parentSection) throws IOException {
        IParticleBoxEmitter emitter = partSystem.createBoxEmitter();
        FileSection settings=parentSection.readSection(parentSection.getChunk("boxEm"));
        vector3df center = settings.readVector(settings.getChunk("center"));
        vector3df size = settings.readVector(settings.getChunk("size"));
        emitter.setCenter(center);
        setEmitterSize(emitter, size);
        partSystem.setEmitter(emitter);
        settings.close();
        return emitter;
    }
}
